package backend;


import java.util.HashMap;
import java.util.Map;

import loaders.ThemeViewer;

import backend.rss.ItemsContainer;
import backend.rss.items.AuthorItem;
import backend.rss.items.DetailedThemeItem;
import backend.rss.items.ThemeItem;

public class ThemeManager
{
	public enum Actions {SEARCH, TAG, POP, RATED, RAND, RECENT, COMMENTS};
	
	private ThemeViewer viewer;
	private Controller controller;
	private ItemsContainer itemsContainer;
	
	private Actions lastActionDone;
	private String lastSearchTerm;
	
	public ThemeManager(ThemeViewer viewer)
	{
		this.viewer = viewer;
		controller 	= new Controller();
	}
	
	public void getNextPage()
	{
		//startIndex will be set to 1 if it is the first page
		int startIndex 	= (itemsContainer.getStartIndex() == 0 ? 1 : itemsContainer.getStartIndex());
		int nextIndex 	= itemsContainer.getStartIndex() + itemsContainer.getItemsPerPage();
		
		movePage(nextIndex);
	}
	
	public void getPreviousPage()
	{
		//int startIndex 	= (itemsContainer.getStartIndex() == 0 ? 1 : itemsContainer.getStartIndex());
		int nextIndex 	= itemsContainer.getStartIndex() - itemsContainer.getItemsPerPage();
		
		movePage(nextIndex);		
	}
	
	private void movePage(int nextIndex)
	{
		//TODO assertion: itemsContainer isn't null and there exists a last action
		doAction(nextIndex);
	}
	
	private void doAction(int nextIndex)
	{
		viewer.displayProgressBar();
		//technically if nextIndex == -1 then it won't be added in.. so we're relying on this bah..
		switch (lastActionDone)
		{
			case SEARCH:
				itemsContainer = controller.defaultSearch(lastSearchTerm, nextIndex);
				break;
			case TAG:
				itemsContainer = controller.searchByTag(lastSearchTerm, nextIndex);
				break;				
			case POP:
				itemsContainer = controller.getRecentPopularThemes(nextIndex);
				break;
			case RATED:
				itemsContainer = controller.getHighestRatedThemes(nextIndex);
				break;
			case RAND:
				itemsContainer = controller.getRandomThemes(nextIndex);
				break;
			case RECENT:
				itemsContainer = controller.getRecentThemes(nextIndex);
				break;
			case COMMENTS:
				itemsContainer = controller.getThemeComments(lastSearchTerm, nextIndex);
			default:
				itemsContainer = controller.getRecentPopularThemes(nextIndex);
		}
		viewer.updateBrowsePanel(itemsContainer);		
		viewer.hideProgressBar();
	}
	
	/**
	 * This is a special case, and does not call doAction. It also does not need to update
	 * the lastActionDone variable
	 * @param themeId
	 */
	public void getDetailedTheme(String themeId)
	{
		viewer.displayProgressBar();
		DetailedThemeItem themeItem = controller.getDetailedTheme(themeId);
		viewer.showThemeDetails(themeItem);
		viewer.hideProgressBar();
	}
	
	public void getThemeComments(String themeId)
	{
		lastActionDone = Actions.COMMENTS;
		//overloading searchTerm to contain themeId
		//TODO refactor
		lastSearchTerm = themeId;
		
		doAction(-1);
	}
	
	public void defaultSearch(String search)
	{
		lastActionDone = Actions.SEARCH;
		lastSearchTerm = search;

		doAction(-1);
	}
	
	public void tagSearch(String search)
	{
		lastActionDone = Actions.TAG;
		lastSearchTerm = search;

		doAction(-1);
	}
	
	public void getRecentPopularThemes()
	{
		lastActionDone = Actions.POP;
	
		doAction(-1);
	}
	
	public void getHighestRatedThemes()
	{
		lastActionDone = Actions.RATED;		
		doAction(-1);
	}
	
	public void getRandomThemes()
	{
		lastActionDone = Actions.RAND;		

		doAction(-1);
	}
	
	public void getRecentThemes()
	{
		lastActionDone = Actions.RECENT;		
		doAction(-1);
	}
}
